#include "TcpServer.h"

TcpServer::TcpServer(const uint16_t port)
{
    acceptor_ = new Acceptor(&loop_, port);
    acceptor_->setnewConnectionCb(std::bind(&TcpServer::newconnection, this, std::placeholders::_1));
}
TcpServer::~TcpServer()
{
    delete acceptor_;
    for (auto &e : conns_)
    {
        delete e.second;
    }
}

void TcpServer::start()
{
    loop_.run();
}

void TcpServer::newconnection(Sock *clientSock)
{
    Connection *conn = new Connection(&loop_, clientSock);
    printf("new connection(fd=%d,ip=%s,port=%d) ok.\n", conn->getFd(), conn->getIp().c_str(), conn->getPort());
    conns_[conn->getFd()]=conn;  
}


